package org.alex.samples.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.alex.samples.dao.IScoreDao;
import org.alex.samples.dao.ISubjectDao;
import org.alex.samples.dao.IUserDao;
import org.alex.samples.model.ExamScore;
import org.alex.samples.model.Subject;
import org.alex.samples.model.SubjectVO;
import org.alex.samples.model.User;
import org.alex.samples.model.UserSubject;
import org.alex.samples.model.UserVO;
import org.alex.samples.service.IUserService;
import org.alex.samples.util.ExcelUtils;
import org.alex.samples.util.MD5Util;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class IUserServiceImpl implements IUserService {

	@Autowired
	private IUserDao userDao;

	@Autowired
	private ISubjectDao subjectDao;
	
	@Autowired
	private IScoreDao soreDao;

	public List<User> findAllUsers() throws Exception {
		return this.userDao.findAllUsers();
	}

	public void createUser(User user) throws Exception {
		this.userDao.createUser(user);
	}

	public UserVO getUserById(String id) throws Exception {
		UserVO userVO = new UserVO();
		UserVO user = this.userDao.getUserById(id);
		if (user != null) {
			BeanUtils.copyProperties(user, userVO);
			return userVO;
		}
		return null;
	}

	public void edtiUser(User user) throws Exception {
		if (user.getPassword().length() <= 16) {
			user.setPassword(MD5Util.toMD5(user.getPassword()));
		}
		this.userDao.editUser(user);
	}

	public void deleteUser(String id) throws Exception {
		this.userDao.deletetUser(id);
	}

	@Override
	public void saveUserByExcel(InputStream inputStream, String postfix) throws Exception {
		Workbook wk = ExcelUtils.openWorkbook(inputStream, postfix);
		List<User> userList = new ArrayList<User>();
		List<UserVO> allExcleUser = ExcelUtils.getUserListByExcel(wk);
		if (allExcleUser != null) {
			// Role role=RoleDao.get(25);
			UserVO user = null;
			for (int i = 0; i < allExcleUser.size(); i++) {
				user = new UserVO();
				BeanUtils.copyProperties(allExcleUser.get(i), user);
				// Set<Role> Roles=new HashSet<Role>();
				// roles.add(role);
				// user.setExamRoles(examRoles);
				userList.add(user);
			}
		}
		userDao.createUsers(userList);
	}

	@Override
	public void updataUserLastLockTime(UserVO user) throws Exception {
		User temp = new User();
		temp.setId(user.getId());
		temp.setLastLockTime(user.getLastLockTime());
		userDao.updataUserLastLockTime(temp);
	}

	@Override
	public void updataUserLoginFailCount(UserVO user) throws Exception {
		User temp = new User();
		temp.setId(user.getId());
		temp.setLoginFailCount(user.getLoginFailCount());
		userDao.updataUserLoginFailCount(temp);
	}

	@Override
	public List<SubjectVO> getSubjectList(UserVO userVO) throws Exception {
		List<SubjectVO> subjects=new ArrayList<SubjectVO>();
		List<UserSubject> userSubjects=userVO.getUserSubjects();
		for(int i = 0;i<userSubjects.size();i++){
			SubjectVO subjectVo=new SubjectVO();
			BeanUtils.copyProperties(userSubjects.get(i).getSubject(),subjectVo);
			ExamScore score=soreDao.getScoreByUserIdAndSubId(userSubjects.get(i).getUserId(),userSubjects.get(i).getSubId());
			subjectVo.setScore(score);
			subjects.add(subjectVo);
		}
		return subjects;
	}


}